<template>
  <view class="wh_750 pageHeight bg_F5F5F5 flex align-center flex-column" title="我的账号">
    <s-header-title is-fixed
                    title="我的账号"
                    title-color="#000000"
                    background="#FFFFFF"
                    @getPageHeight="getPageHeight"
                    is-display-back
    ></s-header-title>
    <view :style="{height: state.pageHeight+ 'px'}"></view>
    <uni-list :border="true" style="background-color: #fff;width: 750rpx;">
      <!-- note="密码:******" -->
      <uni-list-item v-for="item in state.accountList" :thumb="sheep.$url.cdn(item.avatar)"
                     thumb-size="lg" :title="item.account" clickable @click="onItemClick(item)"
                     :rightText="!state.isManager?item.account==userInfo.mobile?'当前账号不可删除':'删除':item.account==userInfo.mobile?'当前账号':'切换'"
                     showArrow />
      <!-- note="点击添加" -->
      <uni-list-item
        thumb="https://img1.baidu.com/it/u=2279237123,3956366328&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
        thumb-size="lg" title="添加新账号" clickable @click="onChangeAccount" showArrow />
    </uni-list>

    <view
      style="width: 100%;display: flex;align-items: center;justify-content: center;margin-top: 50px;margin-bottom: 20rpx;">
      <button class="ss-reset-button save-btn ui-Shadow-Main bg_FF4206" @click="onChangeManager">
        {{ state.isManager ? '管理' : '取消' }}
      </button>
    </view>

  </view>
</template>

<script setup>
  import { computed, ref, reactive, onBeforeMount, unref } from 'vue';
  import { onLoad, onShow } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import SHeaderTitle from '@/sheep/components/s-header-title/s-header-title.vue';

  const state = reactive({
    isManager: true,
    userInfo: {},
    accountList: [],
    pageHeight: 0,
  });
  const userInfo = computed(() => sheep.$store('user').userInfo);
  onLoad(async (options) => {
    uni.$on('login', () => {
      refrashData();
    });
let user = uni.getStorageSync('user-store')
    let accountList = JSON.parse(uni.getStorageSync('account-list') || '[]');
    let arr = accountList.filter((item) => item.account == user.userInfo.mobile);
    if (arr.length == 0) {
      accountList.push({
        account: user.userInfo.mobile,
        avatar: user.userInfo.avatar,
      });
      uni.setStorageSync('account-list', JSON.stringify(accountList));
      state.accountList = accountList;
    }
  });
  onShow(() => {
    state.accountList = JSON.parse(uni.getStorageSync('account-list') || '[]');
  });

  const getPageHeight = (height) => {
    state.pageHeight = height;
  };

  // 修改账号
  const onChangeAccount = () => {
    // #ifndef MP-WEIXIN
    sheep.$router.go('/pages/login/phoneLogin', { is_toggle: 1 });
    // #endif
    // #ifdef MP-WEIXIN
    sheep.$router.go('/pages/login/wechat_login', { is_toggle: 1 });
    // #endif
  };

  // 修改管理状态
  const onChangeManager = () => {
    state.isManager = !state.isManager;
  };

  // 条目点击
  const onItemClick = (item) => {
    var that = this;
    var text = !state.isManager ? item.account == userInfo.value.mobile ? '使用账号不可删除' : '删除' : item.account == userInfo.value.mobile ? '当前账号' : '切换';
    if (text == '使用账号不可删除') {
      sheep.$helper.toast('正在使用的账号不可删除');
      return;
    }
    if (text == '当前账号') {
      sheep.$helper.toast('当前账号已登录');
      return;
    }
    if (text == '删除') {
      uni.showModal({
        title: '提醒',
        content: '是否删除账号:' + item.account,
        cancelText: '取消',
        confirmText: '确定',
        success: async function(res) {
          if (res.confirm) {
            var accountList1 = JSON.parse(uni.getStorageSync('account-list') || '[]');
            accountList1 = accountList1.filter(function(item1) {
              return item1.account !== item.account;
            });
            uni.setStorageSync('account-list', JSON.stringify(accountList1));
            state.accountList = accountList1;
          }
        },
      });
      return;
    }
    if (text == '切换') {
      uni.showModal({
        title: '提醒',
        content: '是否切换到账号:' + item.account,
        cancelText: '取消',
        confirmText: '确定',
        success: async function(res) {
          if (res.confirm) {
            //token设置为空
            uni.setStorageSync('token', '');
            // 提交数据
            const { code, msg } = await sheep.$api.user.smsLogin({
              mobile: item.account,
              code: sheep.$store('app').code,
              is_toggle: 1,
            });
            if (code == 1) {
              await refrashData();
              const userInfo = await sheep.$store('user').getInfo();
              await sheep.$store('AI').setAllSessionList();
              await sheep.$store('AI').setAllFollowList();
              await sheep.$store('family').getList();
              await sheep.$store('family').getFamilyTree();
              const bind_id = computed(() => sheep.$store('AI').bind_id);
              const bind_family_id = computed(() => sheep.$store('AI').bind_family_id);
              if (!!bind_id.value) {
                await sheep.$api.user.bindParent({ parent_id: bind_id.value });
              }
              if (!!bind_family_id.value && !!isLogin.value) {
                const { code } = await sheep.$api.family.applyFamily({ family_id: bind_family_id.value });
                if (code == 1) {
                  sheep.$store('AI').setBindFamilyId('');
                }
              }
            }
          }
        },
      });
      return;
    }
  };

  async function refrashData() {
    // setTimeout(() => {
    //   //#ifdef MP-WEIXIN
    //   uni.redirectTo({
    //     url: '/pages/user/account',
    //   });
    //   //#endif
    //   //#ifdef H5
    //   location.reload();
    //   //#endif
    //   //#ifdef APP
    //   uni.redirectTo({
    //     url: '/pages/user/account',
    //   });
    //   //#endif
    // }, 750);
  };
</script>

<style lang="scss" scoped>

  .save-btn {
    width: 710rpx;
    height: 80rpx;
    border-radius: 40rpx;
    background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
    color: $white;
  }
</style>
